﻿using System.Collections.Generic;
using System.Linq;

namespace Zinnia.Common.DTO.Imp
{
    /// <summary>
    /// 一般响应模型
    /// </summary>
    public class ReponseModel : IReponseModel
    {
        public virtual int code { get; set; }

        public virtual string msg { get; set; }
    }
    /// <summary>
    /// 分页相应模型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class GridReponseModel<T> : ReponseModel, IGridReponseModel<T> where T : new()
    {
        public virtual int count => rows?.Count() ?? 0;
        public virtual int size { get; private set; }

        public virtual int page { get; private set; }

        public virtual int total { get; private set; }

        public virtual int allPages => (total + size - 1) / size;

        public List<T> rows { get; private set; }

        public GridReponseModel<T> SetResult(IQueryable<T> source, int requestPage, int requestSize)
        {
            this.page = requestPage;
            this.size = requestSize;
            if (source.Any())
            {
                this.total = source.Count();
                rows = source.Skip((page - 1) * size).Take(size).ToList();
            }
            return this;
        }
    }
}
